/**
 * @author Dream-Lab مۇختەرجان مەخمۇت
 * @email ug-project@outlook.com
 * @create date 2022-07-24 15:10:13
 * @modify date 2022-07-24 15:10:13
 * @desc [description]
*/



use std::{sync::{Mutex, Arc}, vec, ops::DerefMut, collections::HashMap, time::Instant, hash::Hash};
use tonic::{Request, Response, Status};
use crate::gen::{mini_mulazim::{Empty, GetOrdersOfUserResponse, GetOrdersOfUserRequest, GetOrdersOfRiderRequest, CreateOrderRequest, account_service_server::AccountService, CreateUserRequest, KeysResponse}, entities::{Account, MetaData, Person, Name, Address, Location, Contact}};
use super::{data_container::DataContainer, exts::{IntoTonicResponse, ToOption, ToKeysResponse, GetEntityKey}, structs::GenericEntityIndex};

pub struct AccountServiceImpl {
    pub data: Arc<DataContainer>,
}

#[async_trait::async_trait]
impl AccountService for AccountServiceImpl {
    async fn create(&self, request: Request<CreateUserRequest>) -> Result<tonic::Response<Account> ,tonic::Status> {
        let user_name = request.into_inner().user_name;
        let user = Account {
            meta: Some(MetaData::new_with_default_value()),
            password: format!("PasswordOf{}", user_name.clone()),
            sign_in_name: user_name.clone(),
            person: Some(Person {
                meta: Some(MetaData::new_with_default_value()),
                name: Some(Name {
                    meta: Some(MetaData::new_with_default_value()),
                    ug: user_name.clone(),
                    zh: format!("用户{}", user_name.clone()),
                }),
                address: Some(Address {
                    meta: Some(MetaData::new_with_default_value()),
                    location: Some(Location::default()),
                    city: Some(Name::new("ئۈرۈمچى", "乌鲁木齐")),
                    street: Some(Name::new("تەڭرىتاغ رايۇنى", "天山区"))
                }),
                contacts: Some(Contact {
                    meta: Some(MetaData::new_with_default_value()),
                    email: "mail@main.com".into(),
                    wechat: format!("WeChatOf{}", user_name.clone()),
                    phone_number: format!("PhoneNumberOf{}", user_name.clone()),
                })
            })
        };
        // {
        //     self.data.insert_data(&self.data.orders_of_user, GenericEntityIndex {
        //         key: user.key(),
        //         value: Vec::new(),
        //     });
        // }
        Some(self.data.insert_user(user)).into_tonic_response()
    }

    async fn get_by_id(&self, request:tonic::Request<crate::gen::mini_mulazim::Id> ,) ->  Result<tonic::Response<crate::gen::entities::Account> ,tonic::Status> {
        self.data.get_user_by_id(&request.into_inner().uuid).into_tonic_response()
    }

    async fn update(&self,request:tonic::Request<Account> ,) ->  Result<tonic::Response<crate::gen::entities::Account> ,tonic::Status> {
        self.data.update_user(request.into_inner()).into_option().into_tonic_response()
    }

    async fn list(&self,request:tonic::Request<crate::gen::mini_mulazim::KeysRequest> ,) ->  Result<tonic::Response<crate::gen::mini_mulazim::KeysResponse> ,tonic::Status> {
        self.data.get_keys_of(&self.data.users, &request.into_inner()).into_keys_response().into_option().into_tonic_response()
    }
}
